<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 好友推荐模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class friendsreferral_model extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	/**
	 * 添加 好友推荐 
	 * Enter description here ...
	 * @param  $data Array
	 * 返回  int 类型 0 添加失败 大于0 成功 
	 */
	function addFriendsreferral($data) {
		$this->db->insert ( 'wanwan_enum_friendsreferral', $data );
		return $this->db->insert_id();
		
	}
	/**
	 * 更新好友推荐信息
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 int 类型 0 更新成失败 大于 0执行成功
	 */
	function updateFriendsreferral($data) {
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'wanwan_enum_friendsreferral',$data );
		return $this->db->affected_rows();
	}
	/**
	 * 删除 好友信息 (伪删除)
	 * Enter description here ...
	 * @param  $data  Array
	 * 返回 int 0 伪删除 失败  大于 0 成功
	 */
	function deleteFriendsreferral($data) {
		$deleteData ['deleFlag'] = 1;
		$deleteData ['deleTime'] = date ( 'Y-m-d H:i:s' );
		$this->db->where ( 'friendsreferralId', $data ['friendsreferralId'] );
		$this->db->update ( 'wanwan_enum_friendsreferral', $deleteData );
	}
	/**
	 * 说一下思路 联表查询 首先获得 所在团队 在查处 所在团队的成员 
	 * 在 和wanwan_enum_usertouser   查的结果集 合并 	
	 * Enter description here ...
	 */
	function queryFriendsList($data, $isAll = false) {
		$userId = $data ['userId'];
		$uncheckTeamId = $data ["uncheckTeamId"];
		$datas = array ();
		$item = array ();
		$item1 = array ();
		$unSelectSql = $uncheckTeamId != - 2 ? "and teamid!=$uncheckTeamId" : "";
		$strSame = "";
		$sql = "select teamName,teamIcon,teamId from wanwan_enum_team where teamId in(select teamId from wanwan_enum_usertoteam where userId=$userId ) " . $unSelectSql;
		$result = $this->db->query ( $sql );
		foreach ( $result->result_array () as $row ) {
			$team = array ();
			$team ['label'] = empty ( $row ['teamName'] ) ? '' : $row ['teamName'];
			$team ['value'] = "team";
			$team ['id'] = empty ( $row ['teamId'] ) ? '' : $row ["teamId"];
			$team ['icon'] = empty ( $row ['teamIcon'] ) ? base_url () . 'assets/images/bmlf_04.gif' : base_url () . $row ['teamIcon'];
			$team ['items'] = $this->queryTeamList ( $row ['teamId'], $userId, true, $isAll );
			array_push ( $datas, $team );
		}
		log_message ( "info", "unSelectSql==>" . $unSelectSql );
		if (intval ( $uncheckTeamId ) != - 1) {
			$item ['value'] = "team";
			$item ['label'] = '好友列表';
			$item ['id'] = - 1;
			$item ['icon'] = base_url () . 'assets/images/bmlf_04.gif';
			//assets/images/uploads/20130504045740817200.jpeg
			$item ['items'] = $this->queryTeamList ( $data ["userId"], $userId, false, $isAll );
			array_push ( $datas, $item );
		}
	
		return $datas;
	}
	/**
	 * 判断是否已经有人推荐过，如果存在则
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	function isReferral($data) {
		$userId=intval($data ['userId']);
		$accepterId=intval($data["accepterId"]);
		$sql="SELECT * FROM  wanwan.wanwan_enum_friendsreferral  WHERE (userid=$userId AND accepterId=$accepterId) OR  (userid=$accepterId AND accepterId=$userId) ";
		return $this->db->query($sql)->row_array ();
	
	}
	function getRefreralInfo($data) {
		$userId = intval ( $data ["userId"] );
		$sql = "SELECT a.rfId,a.perserteeName,a.perserteeId,u.nickName,u.userId,u.userBigIcon FROM (SELECT  Id AS rfId ,  CASE userid WHEN $userId THEN accepterId ELSE userid END userId,perserteeName,perserteeId,referralStatus FROM wanwan_enum_friendsreferral WHERE userid=$userId OR accepterId=$userId) a LEFT JOIN wanwan_enum_user u ON a.userId=u.userId where  a.referralStatus=0";
		log_message("info" ,"person ==>".$sql);
		return $this->db->query ( $sql )->result_array ();
	}
	function getReferralUserInfo($data) {
		$Id = intval ( $data ["id"] );
		$sql="SELECT NOW() createTime,ff.userId userId,ff.accepterId friendId,ff.perserteeId referralId,u.nickName friendName,u.userIcon friendIcon,u.sex friendSex,u.tags friendTags FROM wanwan_enum_friendsreferral ff LEFT JOIN wanwan_enum_user u ON ff.accepterId=u.userId WHERE ff.id=$Id";
		return $this->db->query ( $sql )->row_array ();
	}
	/**
	 * 获得团队列表
	 * Enter description here ...
	 */
	function queryTeamList($data, $userId, $isTeam, $isAll = false) {
		//		if (! $isTeam) {
		$id = intval ( $data );
		//		} else {
		//			$teamId = intval ( $data );
		//		}
		//$strCondition = $isAll ? "" : " and referralStatus=1";
		//		$sql = "SELECT u.userId,nickName,userIcon,r.teamId teamId,r.teamName teamName,r.teamIcon teamIcon FROM wanwan_enum_user u LEFT JOIN (
		//			select userid,teamName,teamIcon,teamId from wanwan_enum_usertoteam where teamid in(select teamid from wanwan_enum_usertoteam where userid=$userId) ) r on u.userId=r.userid
		//			where u.userId=$userId union all
		//			SELECT userId,nickName,userIcon,11,'我的好友','ddd' FROM wanwan_enum_user WHERE userid IN (
		//			select distinct case when userid=$userId then friendid else userid end userid from wanwan_enum_usertouser where userid=$userId or friendid=$userId)";
		//		$rs=$this->db->query($sql);
		$joinStr = $isTeam ? "select userid from wanwan_enum_usertoteam where teamid=$id" : "select distinct case when userid=$id then friendid else userid end userid from wanwan_enum_usertouser where userid=$id or friendid=$id";
		$teamSql = "SELECT userId,nickName,userIcon FROM wanwan_enum_user where userid in($joinStr) and userid<> $userId";
		log_message ( "info", "teamSql==>" . $teamSql );
		$teamRs = $this->db->query ( $teamSql );
		$data = array ();
		foreach ( $teamRs->result_array () as $row ) {
			$item = array ();
			$item ['label'] = empty ( $row ['nickName'] ) ? '未知' : $row ['nickName'];
			$item ['value'] = empty ( $row ['userId'] ) ? '未知' : $row ['userId'];
			$item ['icon'] = empty ( $row ['userIcon'] ) ? base_url () . 'assets/images/uploads/defaultIcon.jpeg' : base_url () . $row ['userIcon'];
			array_push ( $data, $item );
		}
		return $data;
	
	}
	
	/**
	 * 添加好友 
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 int  0 失败 大于 0 成功
	 */
	function addFriendInfo($data) {
		$this->db->insert ( 'wanwan_enum_usertouser', $data );
	}
	/**
	 * 
	 * Enter description here ...
	 * @param Array $data
	 */
	function addReferralInfo($data) {
		$this->db->insert ( 'wanwan_enum_friendsreferral', $data );
		$id = $this->db->insert_id ();
		log_message ( "info", "id==>" . $id );
		return $id;
	}
	function addReferral($data) {
		$count = 0;
		$errorMsg = "";
		$friendCount = 0;
		$successMsg = "";
		$code = "success";
		$msg = "";
		$array = explode ( ",", $data ['accepterId'] );
		$referralCount = count ( $array );
		foreach ( $array as $val ) {
			$data ['accepterId'] = $val;
			$selectRs = $this->isReferral ( $data );
			if (empty ( $selectRs )) {
				$data ['createTime'] = date ( 'Y-m-d H:i:s' );
				$data ['referralStatus'] = 0;
				$this->addReferralInfo ( $data );
				$count ++;
				$successMsg = "恭喜你成功推荐" . ($count) . "对好友认识 ";
			} else {
				$friendCount ++;
				$errorMsg = "有" . $friendCount . "对好友推荐失败因为他们认识";
			}
		}
		return array ('msg' => $successMsg . $errorMsg . "." );
	}
	function getAllFriendsInfo($data,$beginNumber=-1,$pageSize=5){
		
		$limit =($beginNumber==-1)?"":"LIMIT $beginNumber,$pageSize";
		$beginNumber--;
		$userId=$data['userId'];
		$sql="SELECT  b.friendTags,b.friendSex,b.id ,a.* ,b.friendName,b.friendIcon,b.friendId  FROM wanwan_enum_user a LEFT JOIN wanwan_enum_usertouser  b ON a.userId=b.userId WHERE  a.userId=$userId AND b.deleFlag=0 ORDER BY a.createTime DESC $limit ";
		return $this->db->query($sql)->result_array();
	}
	//Friendsreferral
}